/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ObjectsOverNet;

import java.net.*;
import java.io.*;

/**
 *
 * @author e.noskov
 */
public class clientTest {

    public static void main(String[] argv) throws Exception {

        long start = System.currentTimeMillis();
        int bytesRead;
        int current = 0;
        // localhost for testing
        Socket sock = new Socket("127.0.0.1", 13267);
        System.out.println("Connecting...");
        
        BufferedReader in = 
                new BufferedReader(new InputStreamReader(sock.getInputStream()));
        
      long fileLength = Long.parseLong(in.readLine());
        
        
        InputStream is = sock.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);
        
        // receive file
        
        
        FileOutputStream fos = new FileOutputStream("in2.mp3");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
         byte[] mybytearray = new byte[1024];
        // thanks to A. Cádiz for the bug fix
        do {
           
            bytesRead =
                    bis.read(mybytearray);
            
           current += mybytearray.length;
            
            if (bytesRead >= 0) {
                
                bos.write(mybytearray);
                
            }
            
        } while (current < fileLength);

        bos.flush();
        long end = System.currentTimeMillis();
        System.out.println(end - start);
        bos.close();
        //sock.close();
    }
}
